package com.esunny.resourcevisualization.service.impl;

import com.esunny.resourcevisualization.domain.entity.Apply;
import com.esunny.resourcevisualization.domain.entity.ResourceCacheInfo;
import com.esunny.resourcevisualization.domain.request.ApplyInfoDto;
import com.esunny.resourcevisualization.domain.response.ReturnStructure;
import com.esunny.resourcevisualization.manage.ApplyManage;
import com.esunny.resourcevisualization.manage.ResourceCacheInfoManage;
import com.esunny.resourcevisualization.service.ApplyService;
import com.esunny.resourcevisualization.service.ProcessService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @version 1.0.0.0
 * @ClassName ResourceCacheInfoServiceImpl
 * @Description 资源缓存信息处理逻辑实现
 * @Author baochencong
 * @Date 2020/11/26 8:12
 */
@Service
public class ApplyServiceImpl implements ApplyService {

    @Resource
    private ResourceCacheInfoManage resourceCacheInfoManage;

    @Resource
    private ApplyManage applyManage;

    @Resource
    private ProcessService processService;

    @Override
    public ReturnStructure addApplyInfo(ApplyInfoDto applyInfoDto) {
        Apply applyInfo = applyInfoDto.getApplyInfo();
        if (applyInfo.getApplyResourceId() == 0) {
            ResourceCacheInfo resourceCacheInfo = applyInfoDto.getResourceCacheInfo();
            resourceCacheInfoManage.addResourceCacheInfo(resourceCacheInfo);
            applyInfo.setResourceCacheInfoId(resourceCacheInfo.getId());
        }
        applyManage.addApplyInfo(applyInfo);

        return processService.submitApply(applyInfo.getId());
    }

    @Override
    public ReturnStructure updateApplyInfo(ApplyInfoDto applyInfoDto) {
        if (!judgeApplyInfoDtoIsValid(applyInfoDto)) {
            return ReturnStructure.NOT_FOUND_APPLY();
        }
        if (applyInfoDto.getApplyInfo().getApplyResourceId() == 0) {
            resourceCacheInfoManage.updateResourceCacheInfoById(applyInfoDto.getResourceCacheInfo());
        }
        applyManage.updateApplyInfoById(applyInfoDto.getApplyInfo());

        return ReturnStructure.SUCCESS("修改申请信息成功", null);
    }

    @Override
    public ReturnStructure remarkDeleteApplyInfoById(int applyId) {
        Apply applyInfo = applyManage.getApplyInfoById(applyId);
        if (applyInfo == null) {
            return ReturnStructure.NOT_FOUND_APPLY();
        }
        applyManage.remarkDeleteApplyInfo(applyId);
        return ReturnStructure.SUCCESS("删除申请信息成功", null);
    }

    @Override
    public List<Apply> getApplyListInfoByApplyBy(String applyBy) {
        return applyManage.getApplyListInfoByApplyBy(applyBy);
    }

    @Override
    public List<Apply> getApplyListInfoByAuditor() {
        List<Apply> applyListInfo = new ArrayList<>();
        List<String> processInstanceIdList = processService.getProcessInstanceIdListByAuditor();
        for (String processInstanceId : processInstanceIdList) {
            Apply applyInfo = applyManage.getApplyInfoByProcessInstanceId(processInstanceId);
            if (applyInfo != null) {
                applyListInfo.add(applyInfo);
            }
        }
        return applyListInfo;
    }

    @Override
    public List<Apply> getApplyInfoList() {
        return applyManage.getApplyInfoList();
    }

    @Override
    public Apply getApplyInfoById(int applyId) {
        return applyManage.getApplyInfoById(applyId);
    }

    private boolean judgeApplyInfoDtoIsValid(ApplyInfoDto applyInfoDto) {
        return applyManage.getApplyInfoById(applyInfoDto.getApplyInfo().getId()) != null;
    }

}
